var express = require('express');
var router = express.Router();
const { devPath } = require('../util');
const { getBlocks } = require('../controller/block');
const { engine } = require('../util/liquid');
const { globalCache } = require('../util/memoryCache');
const { setCookieTheme, setCheckoutCookieTheme } = require('../middleware/theme');
const { getThemeConfig } = require('../service/requestAdmin');
/**
 * 统一页面数据设置方法
 * @param {string} current_route - 当前路由，也是缓存的key
 * @param {string} template - 模板名称
 */
function setPageGlobals(current_route, template) {
  // 设置基础路由信息
  engine.options.globals['routes'] = {
    current_route: current_route,
    current_route_handle: '',
  };

  // 设置模板信息
  engine.options.globals['templates'] = template;
  engine.options.globals['template'] = template;

  // 从 globalCache 中获取页面数据
  const pageData = globalCache.get(current_route);
  if (pageData) {
    // 如果缓存中有数据，合并到 globals
    Object.assign(engine.options.globals, pageData);
  }
  // 如果缓存中没有数据，不做任何操作（空对象）
}

//首页
router.get('/', setCookieTheme, async function (req, res) {
  setPageGlobals('index/index', 'index');
  engine.options.globals['theme_config'] = await getThemeConfig('index/index', '');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/block', function (req, res) {
  engine.options.globals['blockList'] = getBlocks();
  engine.options.globals['isBlock'] = true;
  res.render('block.liquid');
});

router.get('/coupons', setCookieTheme, function (req, res) {
  setPageGlobals('coupon/list', 'coupon_list');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/promotions', setCookieTheme, function (req, res) {
  setPageGlobals('promotion/list', 'promotion_list');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/promotions/:id', setCookieTheme, function (req, res) {
  setPageGlobals('promotion/detail', 'promotion_detail');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/coupons/:id', setCookieTheme, function (req, res) {
  setPageGlobals('coupon/detail', 'coupon_detail');
  res.render(devPath('/layout/theme.liquid'));
});

//注册页
router.get('/account/register', setCookieTheme, function (req, res) {
  setPageGlobals('account/register', 'register');
  res.render(devPath('/layout/theme.liquid'));
});

//博客专辑
router.get('/news', setCookieTheme, function (req, res) {
  setPageGlobals('news', 'news');
  res.render(devPath('/layout/theme.liquid'));
});

//博客列表
router.get('/blogs', setCookieTheme, function (req, res) {
  setPageGlobals('blog/list', 'blog_list');
  res.render(devPath('/layout/theme.liquid'));
});

//博客专辑详情
router.get('/news/:id', setCookieTheme, function (req, res) {
  setPageGlobals('news/detail', 'news_detail');
  res.render(devPath('/layout/theme.liquid'));
});

//所有评论页
router.get('/comments', setCookieTheme, function (req, res) {
  setPageGlobals('comments/list', 'comments_list');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/collections/all', setCookieTheme, function (req, res) {
  setPageGlobals('collection/list', 'collection_list');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/collections/:id', setCookieTheme, function (req, res) {
  setPageGlobals('collection/detail', 'collection_detail');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/account/trackingorder', setCookieTheme, function (req, res) {
  setPageGlobals('account/trackingorder', 'customer_trackingorder');
  res.render(devPath('/layout/theme.liquid'));
});

router.get('/account/login', setCookieTheme, function (req, res) {
  setPageGlobals('account/login', 'customer_login');
  res.render(devPath('/layout/theme.liquid'));
});

//商品页
router.get('/products/:id', setCookieTheme, function (req, res) {
  setPageGlobals('product/detail', 'product_detail');

  if (req.query.quick_view === '1') {
    res.render(devPath('/layout/template.liquid'));
  } else {
    res.render(devPath('/layout/theme.liquid'));
  }
});

//购物车
router.get('/cart', setCookieTheme, function (req, res) {
  setPageGlobals('cart', 'cart');
  console.log(req.params.id);
  res.render(devPath('/layout/theme.liquid'));
});

//结算页
router.get('/checkouts/:id', setCheckoutCookieTheme, function (req, res) {
  setPageGlobals('order/checkout', 'checkout');
  res.render('./src/theme/system/layout/checkout.liquid');
});

//单页结算页
router.get('/:id/one-page-checkouts/:token', setCheckoutCookieTheme, function (req, res) {
  setPageGlobals('orderonepage/index', 'orderonepage_index');
  engine.options.globals['checkout_token'] = req.params.token;
  res.render('./src/theme/system/layout/checkout.liquid');
});

// 进阶方式结账
router.get('/:id/single-page-checkouts/:token', setCheckoutCookieTheme, async function (req, res) {
  setPageGlobals('ordersinglepage/index', 'ordersinglepage_index');
  engine.options.globals['checkout_token'] = req.params.token;
  res.render('./src/theme/system/layout/checkout.liquid');
});
// COD方式结账
router.get('/:id/cod-checkouts/:token', setCheckoutCookieTheme, async function (req, res) {
  setPageGlobals('ordercodpage/index', 'ordercodpage_index');
  engine.options.globals['checkout_token'] = req.params.token;
  res.render('./src/theme/system/layout/checkout.liquid');
});

// 进阶方式结账网管也需要渲染结算页
router.get('/:id/ordersinglepage_gateway/:token', setCheckoutCookieTheme, async function (req, res) {
  setPageGlobals('ordersinglepage/index', 'ordersinglepage_index');
  engine.options.globals['checkout_token'] = req.params.token;
  res.render('./src/theme/system/layout/checkout.liquid');
});

//订单详情页
router.get('/module/:id', setCheckoutCookieTheme, function (req, res) {
  console.log(req.params.id);
  setPageGlobals('account/orderdetail', 'order_detail');
  res.render(devPath('/layout/theme.liquid'));
});

module.exports = router;
